package datetest;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

//可视化日历程序
public class VisualCalendar {
    public static void main(String[] args) {
        System.out.println("请输入yyyy-MM-dd格式的日期:");
        Scanner scanner=new Scanner(System.in);
        String str=scanner.next();
        String temp=str;
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date=format.parse(temp);
            Calendar c=new GregorianCalendar();
            c.setTime(date);
            int day=c.get(Calendar.DATE);//获取当前日期

            c.set(Calendar.DATE,1);//把日期变为1号
            //System.out.println(c.get(Calendar.DAY_OF_WEEK));//1号是星期几
            //System.out.println(c.getActualMaximum(Calendar.DATE));//一个月最多有几个月
            int firstDate=c.get(Calendar.DAY_OF_WEEK);
            int maxDate=c.getActualMaximum(Calendar.DATE);
            System.out.println("日\t一\t二\t三\t四\t五\t六");
            for (int i=1;i<firstDate;i++){
                System.out.print("\t");
            }
            for (int i=1;i<=maxDate;i++){
                if (i==day){
                    System.out.print("*");
                }
                System.out.print(i+"\t");
                int week=c.get(Calendar.DAY_OF_WEEK);//判断每个日期对应的星期
                if (week==Calendar.SATURDAY){
                    System.out.println();
                }
                c.add(Calendar.DATE,1);//每循环一次日期加1
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
